package es.udc.fic.hamNeggs.dao;

import es.udc.fic.hamNeggs.manager.MeetingManager;
import es.udc.fic.hamNeggs.manager.UserStoriesManager;
import es.udc.fic.hamNeggs.manager.WorksManager;
import es.udc.fic.hamNeggs.pojo.Meeting;
import es.udc.fic.hamNeggs.pojo.User;
import es.udc.fic.hamNeggs.pojo.UserStories;
import es.udc.fic.hamNeggs.pojo.Works;

import java.util.ArrayList;

public class InputUser {
    private String                  id;

    private String                  name;

    private String                  fullName;

    private String                  password;

    private String                  currentRol;

    private ArrayList<String>       meetings       = new ArrayList<String>();

    private ArrayList<String>       works          = new ArrayList<String>();

    private ArrayList<String>       goesToMeetings = new ArrayList<String>();

    private final ArrayList<String> userStories    = new ArrayList<String>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public ArrayList<String> getMeetings() {
        return meetings;
    }

    public void setMeetings(ArrayList<String> meetings) {
        this.meetings = meetings;
    }

    public ArrayList<String> getWorks() {
        return works;
    }

    public void setWorks(ArrayList<String> works) {
        this.works = works;
    }

    public ArrayList<String> getGoesToMeetings() {
        return goesToMeetings;
    }

    public void setGoesToMeetings(ArrayList<String> goesToMeetings) {
        this.goesToMeetings = goesToMeetings;
    }

    public ArrayList<String> getUserStories() {
        return userStories;
    }

    public String getCurrentRol() {
        return currentRol;
    }

    public void setCurrentRol(String currentRol) {
        this.currentRol = currentRol;
    }

    public User toUser() {
        User user = new User();
        MeetingManager mtgmng = new MeetingManager();
        WorksManager wrkmng = new WorksManager();
        UserStoriesManager ustmng = new UserStoriesManager();

        if (id != null && id != "")
            user.setId(new Long(id));
        if (name != null && name != "")
            user.setName(name);
        if (fullName != null && fullName != "")
            user.setFullName(fullName);
        if (password != null && password != "")
            user.setPassword(password);
        if (meetings != null && !meetings.isEmpty())
            for (String mtg : meetings)
                user.setOneLeaderMeeting(mtgmng.getMeeting(new Long(mtg)));
        if (goesToMeetings != null && !goesToMeetings.isEmpty())
            for (String mtg : meetings)
                user.setOneGoesMeeting(mtgmng.getMeeting(new Long(mtg)));
        if (works != null && !works.isEmpty())
            for (String wrk : works)
                user.setOneWorks(wrkmng.getWorks(new Long(id), new Long(wrk)));
        if (userStories != null && !userStories.isEmpty())
            for (String ust : userStories)
                user.setOneUserStories(ustmng.getUserStories(new Long(ust)));

        mtgmng.close();
        wrkmng.close();
        ustmng.close();

        return user;
    }

    public void setUser(User user) {
        if (user.getId() != null)
            id = user.getId().toString();
        if (user.getName() != null)
            name = user.getName();
        if (user.getFullName() != null)
            fullName = user.getFullName();
        if (user.getPassword() != null)
            password = user.getPassword();
        if (user.getMeetings() != null && !user.getMeetings().isEmpty())
            for (Meeting mtg : user.getMeetings())
                meetings.add(mtg.getId().toString());
        if (user.getGoesToMeetings() != null && !user.getGoesToMeetings().isEmpty())
            for (Meeting mtg : user.getGoesToMeetings())
                goesToMeetings.add(mtg.getId().toString());
        if (user.getWorks() != null && !user.getWorks().isEmpty())
            for (Works wrk : user.getWorks())
                works.add(wrk.getProject().getId().toString());
        if (user.getUserStories() != null && !user.getUserStories().isEmpty())
            for (UserStories ust : user.getUserStories())
                userStories.add(ust.getId().toString());
    }
}
